<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键字：">
          <el-input v-model="listPram.keyword" placeholder="商家电话/商家名称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label=" 营业状态:">
          <el-select v-model="listPram.status" placeholder="请选择" style="width: 200px;">
            <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.data"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区:" prop="areaId">
          <el-cascader class="col"
                       placeholder="可输入地区名称进行搜索"
                       v-model="value"
                       :options="options"
                       clearable
                       :props="{ expandTrigger: 'hover' }"
                       filterable
                       @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openEdit()"
                     v-has-permi="['admin:business:save']">添加
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}"
                border size="mini">
        <el-table-column label="ID" prop="id" min-width="35"></el-table-column>
        <el-table-column label="商家名称" min-width="130" prop="businessName"/>
        <el-table-column label="所在地区" min-width="100" prop="areaText"/>
        <el-table-column label="商家电话" min-width="80" prop="businessPhone"/>
        <el-table-column label="商家标签" min-width="80" prop="label"/>
        <el-table-column label="商家地址" min-width="180" prop="address"/>
        <el-table-column label="营业时间" min-width="60" prop="businessTime"/>
        <el-table-column label="商家类型" min-width="50" prop="typeText"/>
        <el-table-column label="营业状态" prop="businessStatus" min-width="50">
          <template slot-scope="scope">
            <div style="color:yellowgreen;" v-if="scope.row.businessStatus===1">营业中</div>
            <div style="color:#FF8D02;" v-if="scope.row.businessStatus===2">未营业</div>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" min-width="250">
          <template slot-scope="scope">
            <el-button v-has-permi="['admin:merchantActivities:grant']" icon="el-icon-document-remove" size="mini"
                       type="success"
                       @click="openGrant(scope.row.id,scope.row)">发放
            </el-button>
            <el-button v-has-permi="['admin:merchantActivities:list']" icon="el-icon-document-remove" size="mini"
                       type="warning"
                       @click="openEditList(scope.row.id,scope.row)">商家活动
            </el-button>
            <el-button v-has-permi="['admin:business:save']" icon="el-icon-edit" size="mini" type="primary"
                       @click="openEdit(scope.row.id)">编辑
            </el-button>
            <el-button v-has-permi="['admin:business:delete']" icon="el-icon-delete" size="mini" type="danger"
                       @click="handlerDelete(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          :total="listData.count"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :close-on-click-modal="false"
        :title="editDialog.id? '编辑':'新增'"
        :visible.sync="editDialog.show"
        destroy-on-close
        top="60px"
        width="1200px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          :businessTypeList="editDialog.businessTypeList"
          ref="editForm"
          @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
    <el-dialog
        :close-on-click-modal="false"
        title="活动列表"
        :visible.sync="editDialogList.show"
        destroy-on-close
        top="80px"
        width="1200px">
      <merchantActivitiesList
          v-if="editDialogList.show"
          :id="editDialogList.id"
          :edit-data="editDialogList.editDate"
          ref="editForm"
          @editListDialog="editListDialog"
      />
    </el-dialog>
    <el-dialog
        :close-on-click-modal="false"
        title="发放益联券"
        :visible.sync="grant.show"
        destroy-on-close
        top="80px"
        width="1200px">
      <grant
          v-if="grant.show"
          :id="grant.id"
          :edit-data="grant.editDate"
          ref="editForm"
          @hideGrantDialog="hideGrantDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import {deleteRow, getBusinessTypeList, list, optionsList, updateStatus} from "@/api/business";
import {checkPermi} from "@/utils/permission";
import merchantActivitiesList from "@/views/operate/business/merchantActivities/index.vue";
import Grant from "@/views/operate/business/list/grant.vue";

export default {
  components: {Grant, edit, merchantActivitiesList},
  data() {
    return {
      listData: {list: [], count: 0},
      listPram: {
        page: 0,
        limit: 20,
        keyword:'',
        areaId:'',
        status:'',
        phone:''
      },
      editDialog: {
        show: false,
        id: null,
        businessTypeList:[],
      },
      editDialogList: {
        show: false,
        id: null,
        editDate: {},
        pid: null
      },
      grant: {
        show: false,
        id: null,
        editDate: {},
        pid: null
      },
      tableLoading: false,
      statusList: [{id: 1, data: '营业中'}, {id: 2, data: '未营业'}],
      imageUrl: '',
      value: [],
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
    this.getOptions()
  },
  methods: {
    handleChange(value) {
      this.listPram.areaId = value[2]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    getBusinessTypeList(){
      getBusinessTypeList().then(res =>{
        this.editDialog.businessTypeList = res
      })
    },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
      this.getBusinessTypeList();
    },
    openEditList(id = null, editDate) {
      this.editDialogList.id = id;
      this.editDialogList.show = true
      this.editDialogList.editDate = editDate
    },
    openGrant(id = null, editDate) {
      this.grant.id = id;
      this.grant.show = true
      this.grant.editDate = editDate
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideGrantDialog() {
      this.grant.show = false
      this.getList()
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    editListDialog() {
      this.editDialogList.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(status, id) {
      const loading = this.$loading({text: '更新中', background: 'rgba(255,255,255,.5)'})
      updateStatus(id, status).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
    resetQuery() {
      this.listPram.keyword = '';
      this.listPram.status = null;
      this.listPram.areaId = '';
      this.value = '';
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>

<style scoped>

</style>
